<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ include file="../session.jsp"%>
<!DOCTYPE html>
<html lang="ko">
  <head>
  	<%@ include file="../head.jsp"%>${alert}
  	<style>
  		.input-group-addon {
  			background-color: #ffffff;
  			border: 0px;
  		}
  		#checkDelete {
  			margin-bottom: 10px;
  		}
  	</style>
  </head>
  <body>
  	<%@ include file="../menu.jsp"%>
	<div class="container">
		<div class="page-header clearfix">
			<h2 class="pull-left">추천브랜드 관리</h2>
			<div class="pull-right" style="padding-top: 30px">
				<a href="#" id="opensearch" class="btn btn-info">추천브랜드 등록 펼치기/접기</a>
			</div>
		</div>
		
		<div id="searchdiv">
			<div class="clearfix">
				<div class="table-responsive">
					<form id="insertRecommendBrand" class="form-horizontal" role="form" method="get" action="recommendBrand.do">
						<table class="table table-bordered">
							<thead>
								<tr class="success">
									<th class="text-center col-md-1">노출순서</th>
									<th class="text-center col-md-2">브랜드명</th>
									<th class="text-center col-md-5">프로모션 텍스트</th>
									<th class="text-center col-md-2">노출기간</th>
									<th class="text-center col-md-2">등록</th>
								</tr>
							</thead>
							<tbody>
								<tr class="text-center">
									<td class="col-md-1"><input type="number" class="form-control" name="insertSortKey"/></td>
									<td class="col-md-2">
										<select name="insertBrand" class="form-control">
											<option>- 선택하세요 -</option>
											<c:forEach items="${brand}" var="brand">
												<option value="${brand.idBrand}">${brand.brandTitle}</option>
											</c:forEach>
										</select>
									</td>
									<td class="col-md-5"><input type="text" class="form-control" name="insertPromotionText"/></td>
									<td class="col-md-2">
										<div class="input-group input-group-sm">
											<input type="datetime-local" class="form-control" name="insertStart_date">
											<span class="input-group-addon">~</span>
											<input type="datetime-local" class="form-control" name="insertEnd_date">
										</div>
									</td>
									<td class="col-md-2"><button type="submit" class="btn btn-primary btn-block btn-sm">등록완료</button></td>
								</tr>
							</tbody>
						</table>
					</form>
				</div>
				<hr/>
			</div>
		</div>
		
		<div class="table-responsive">
			<span class="pull-left">등록 : ${page.totalArticleCount}건</span>
			<form id="deleteRecommendBrand" role="form" method="get" action="recommendBrand.do"></form>
			<button type="submit" class="btn btn-danger pull-right" form="deleteRecommendBrand" id="checkDelete">선택삭제</button>
			<table class="table table-striped table-hover">
				<thead>
					<tr class="warning row">
						<th class="text-center col-md-1">선택</th>
						<th class="text-center col-md-1">노출순서</th>
						<th class="text-center col-md-2">브랜드</th>
						<th class="text-center col-md-4">프로모션텍스트</th>
						<th class="text-center col-md-2">노출기간</th>
						<th class="text-center col-md-1">배너</th>
						<th class="text-center col-md-1">저장</th>
					</tr>
				</thead>
				<tbody>
					<c:forEach items="${recommendBrand}" var="list">
						<form id="updateRecommendBrand${list.idBrand}" role="form" method="post" action="recommendBrand.do"></form>
						<input type="hidden" name="id" value="${list.idBrand}" form="updateRecommendBrand${list.idBrand}"/>
						<input type="hidden" name="page" value="${page.nowPage}" form="updateRecommendBrand${list.idBrand}"/>
						<tr class="text-center row">
							<td class="col-md-1">
								<input type="checkbox" name="delete_list" value="${list.idBrand}" form="deleteRecommendBrand"/>
							</td>
							<td class="col-md-1">
								<input type="text" class="form-control" name="sortKey${list.idBrand}" value="${list.sortKey}" form="updateRecommendBrand${list.idBrand}"/>
							</td>
							<td class="col-md-2">${list.brandTitle}</td>
							<td class="col-md-4">
								<input type="text" class="form-control" name="promotionText${list.idBrand}" value="${list.promotionText}" form="updateRecommendBrand${list.idBrand}"/>
							</td>
							<td class="col-md-2">
								<div class="input-group input-group-sm">
									<input type="datetime-local" class="form-control" name="start_date${list.idBrand}" form="updateRecommendBrand${list.idBrand}" 
									value="${fn:substring(list.start_date, 0, 4)}-${fn:substring(list.start_date, 4, 6)}-${fn:substring(list.start_date, 6, 8)}T${fn:substring(list.start_date, 8, 10)}:${fn:substring(list.start_date, 10, 12)}">
									<span class="input-group-addon">~</span>
									<input type="datetime-local" class="form-control" name="end_date${list.idBrand}" form="updateRecommendBrand${list.idBrand}" 
									value="${fn:substring(list.end_date, 0, 4)}-${fn:substring(list.end_date, 4, 6)}-${fn:substring(list.end_date, 6, 8)}T${fn:substring(list.end_date, 8, 10)}:${fn:substring(list.end_date, 10, 12)}">
								</div>
							</td>
							<td class="col-md-1">
								<a href="#" onclick="window.open('brandBanner.do?id=${list.idBrand}&name=${list.brandTitle}', '', 'width=1200, height=800, scrollbars=yes, toolbar=no, menubar=no, status=no, location=no'); return false;">
									<button type="button" class="btn btn-defalut btn-block btn-sm">배너관리</button>
								</a>
							</td>
							<td class="col-md-1">
								<button type="submit" class="btn btn-warning btn-block btn-sm" form="updateRecommendBrand${list.idBrand}">수정완료</button>
							</td>
						</tr>
					</c:forEach>
				</tbody>
			</table>
		</div>
		
		<div class="text-center">
			<ul class="pagination">

				<!-- 이전 그룹 -->
				<c:choose>
					<c:when test="${page.startPage > page.pageGroupCount}">
						<li><a href="recommendBrand.do?page=${page.startPage-1}">&laquo;</a></li>
					</c:when>
					<c:otherwise>
						<li class="disabled"><a href="#">&laquo;</a></li>
					</c:otherwise>
				</c:choose>
				
				<!-- 페이지 번호 -->
				<c:forEach begin="${page.startPage}" end="${page.endPage}" step="1" var="now">
					<c:choose>
						<c:when test="${page.nowPage == now}">
							<li class="active"><a href="#">${now}</a></li>
						</c:when>
						<c:otherwise>
							<li><a href="recommendBrand.do?page=${now}">${now}</a></li>
						</c:otherwise>
					</c:choose>
				</c:forEach>

				<!-- 다음 그룹 -->
				<c:choose>
					<c:when test="${page.endPage < page.lastGroupFirstPage}">
						<li><a href="recommendBrand.do?page=${page.endPage+1}">&raquo;</a></li>
					</c:when>
					<c:otherwise>
						<li class="disabled"><a href="#">&raquo;</a></li>
					</c:otherwise>
				</c:choose>

			</ul>
		</div>
		
	</div>
	<!-- Javascript -->
	<script src="assets/js/jquery.js"></script>
	<script src="assets/js/bootstrap.min.js"></script>
	<!-- 형식검사 클래스 참조 -->
	<script src="assets/js/string.js"></script>
	<script type="text/javascript">
		$(function() {
			$("#searchdiv").hide();
			
			$("#opensearch").click(function() {
				$("#searchdiv").toggle();
				return false;
			});
			
			$("#insertRecommendBrand").submit(function() {
				if (!$("input[name='insertSortKey']").val().isValue()) {
					alert("노출순서를 입력하세요.");
					$("input[name='insertSortKey']").focus();
					return false;
				}
				
				if ($("select[name='insertBrand'] > option:selected").index() < 1) {
					alert("브랜드를 선택하세요.");
					$("select[name='insertBrand']").focus();
					return false;
				}
				
				if (!$("input[name='insertPromotionText']").val().isValue()) {
					alert("프로모션 텍스트를 입력하세요.");
					$("input[name='insertPromotionText']").focus();
					return false;
				}
				
				if (!$("input[name='insertStart_date']").val().isValue()) {
					alert("시작일을 입력하세요.");
					$("input[name='insertStart_date']").focus();
					return false;
				}
				
				if (!$("input[name='insertEnd_date']").val().isValue()) {
					alert("종료일를 입력하세요.");
					$("input[name='insertEnd_date']").focus();
					return false;
				}
				
				if (!confirm("정말 등록하시겠습니까?")) {
					return false;
				}
			});
			
			<c:forEach items="${recommendBrand}" var="list">
				$("#updateRecommendBrand${list.idBrand}").submit(function() {
				
					if (!$("input[name='sortKey${list.idBrand}']").val().isValue()) {
						alert("노출순서를 입력하세요.");
						$("input[name='sortKey${list.idBrand}']").focus();
						return false;
					}
					
					if (!$("input[name='sortKey${list.idBrand}']").val().isNumber()) {
						alert("정렬순서는 숫자로만 입력이 가능합니다.");
						$("input[name='sortKey${list.idBrand}']").focus();
						return false;
					}
					
					if (!$("input[name='promotionText${list.idBrand}']").val().isValue()) {
						alert("프로모션 텍스트를 입력하세요.");
						$("input[name='promotionText${list.idBrand}']").focus();
						return false;
					}
					
					if (!$("input[name='start_date${list.idBrand}']").val().isValue()) {
						alert("시작일을 입력하세요.");
						$("input[name='start_date${list.idBrand}']").focus();
						return false;
					}
					
					if (!$("input[name='end_date${list.idBrand}']").val().isValue()) {
						alert("종료일를 입력하세요.");
						$("input[name='end_date${list.idBrand}']").focus();
						return false;
					}
					
					if (!confirm("정말 수정하시겠습니까?")) {
						return false;
					}
				});
			</c:forEach>
			
			$("#deleteRecommendBrand").submit(function() {
				if (!$("input[name='delete_list']").is(":checked")) {
					alert("삭제할 추천브랜드의 체크박스를 선택하세요.");
					return false;
				}
				
				if (!confirm("정말 삭제하시겠습니까?")) {
					return false;
				}
			});
		});
	</script>
  </body>
</html>